package com.bdzl.auth.controller;


import com.bdzl.auth.service.CheckService;
import com.bdzl.pub.entity.sys.UserDO;
import com.bdzl.pub.util.param.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
* @Description: TODO 登录及权限校验
* @Author: Ming
* @Date: 2021/4/29 17:06
*/
@RestController
@RequestMapping("/check")
@Slf4j
public class CheckController {

	@Autowired
	private CheckService checkService;

	@PostMapping("/checkLogin")
	public Object checkLogin() {
		return checkService.checkLogin();
	}

	/**
	* @Description: TODO 获取登录用户的数据
	* @return: java.lang.Object
	* @Author: Ming
	* @Date: 2021/4/29 17:46
	*/
	@PostMapping(value="/getUserInfo")
	public R getUserInfo(String token) {
		/*ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();*/
		UserDO user = checkService.getUserInfo(token);
		if(null == user || user.getUserId()<= 0) {
			return R.error();
		}
		return R.ok(user);
	}

	/**
	* @Description: TODO  验证权限 会先验证登录状态
	* @param checkUrl
	* @return: com.bdzl.pub.util.param.R
	* @Author: Ming
	* @Date: 2021/4/29 17:49
	*/
	@PostMapping(value="/checkPermission")
	public R checkPermission(@RequestParam("token")String token,@RequestParam("checkUrl")String checkUrl) {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		return checkService.checkPermission(request,token, checkUrl);
	}


}
